#!/bin/bash
source /opt/root/install/bin/thisroot.sh
source /opt/geant4/install/bin/geant4.sh

cd B1
mkdir build
cd build
apt update && apt install -y gcovr lcov cppcheck
cmake -DENABLE_COVERAGE=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
cppcheck --enable=all --std=c++17 --inconclusive --xml --xml-version=2 --project=compile_commands.json -ibuild -iCMakeFiles .. 2> cppcheck.xml
if [ $? != 0 ]; then
    exit 1
fi
match='\/run\/beamOn'
replacement='\/run\/beamOn 1'
file=run1.mac
sed -i "/$match/s/.*/$replacement/" $file
match='\/run\/numberOfThreads'
replacement='\/run\/numberOfThreads 1'
file=run1.mac
sed -i "/$match/s/.*/$replacement/" $file

match='\/run\/beamOn'
replacement='\/run\/beamOn 1'
file=run2.mac
sed -i "/$match/s/.*/$replacement/" $file
match='\/run\/numberOfThreads'
replacement='\/run\/numberOfThreads 1'
file=run2.mac
sed -i "/$match/s/.*/$replacement/" $file

if [ $? != 0 ]; then
    exit 1
fi
make
if [ $? != 0 ]; then
    exit 1
fi
./exampleB1 run1.mac
if [ $? != 0 ]; then
    exit 1
fi
./exampleB1 run2.mac
if [ $? != 0 ]; then
    exit 1
fi
gcovr --xml-pretty -r .. --output coverage.xml
cd ${WORKSPACE}
